//
//  Record.m
//  MoneyLog
//
//  Created by wicresoft on 11-1-27.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Record.h"
#import "RecordWrapper.h"

static NSMutableDictionary *records;

@implementation Record

@synthesize name;
@synthesize date;
@synthesize recordWrappers;

/*
 Class methods to manage global records.
 */
+ (void)initialize {
	records = [[NSMutableDictionary alloc] init];	
}

+ (Record *)recordNamed:(NSString *)name {
	return [records objectForKey:name];
}

+ (Record *)newRecordWithName:(NSString *)recordName {
    // Create a new record with a given name; add it to the records dictionary.
	Record *newRecord = [[Record alloc] init];
	newRecord.name = recordName;
	NSMutableArray *array = [[NSMutableArray alloc] init];
	newRecord.recordWrappers = array;
	[array release];
	[records setObject:newRecord forKey:recordName];
	
	return newRecord;
}

+ (void) recordRemoveAll {
	[records removeAllObjects];
}

- (void)addRecordWrapper:(NSString	*)name recordWrapper:(RecordWrapper *)recordWrapper {
    // Add a recordWrapper to the record; use nameComponents since that's expensive to compute.
	[recordWrappers addObject:recordWrapper]; 
}

- (void)sortRecordWrapper {
    // Sort the zone wrappers by locale name.
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"recordWrapperId" ascending:YES];
	NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
	[recordWrappers sortUsingDescriptors:sortDescriptors];
	[sortDescriptors release];
	[sortDescriptor release];
}

-(void)dealloc{
	[name release];
	[date release];
	[recordWrappers release];
	[super dealloc];
}

@end
